import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

import { Route_White_List } from '@/const'

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  // 登录
  {
    path: '/login',
    component: (resolve) => require(["@/views/system/login"], resolve),
  },
  // 注册
  {
    path: '/register',
    component: (resolve) => require(["@/views/system/register"], resolve),
  },
  // 首页
  {
    path: '/home',
    component: (resolve) => require(["@/views/home"], resolve),
  },
  // 日记模块
  {
    path: '/diary',
    component: (resolve) => require(["@/views/diary"], resolve),
    children: [
      {
        path: '',
        component: (resolve) => require(["@/views/diary/diary"], resolve),
      },
      {
        path: 'add',
        component: (resolve) => require(["@/views/diary/add"], resolve),
      },
      {
        path: 'edit',
        component: (resolve) => require(["@/views/diary/edit"], resolve),
      },
      {
        path: 'detail',
        component: (resolve) => require(["@/views/diary/detail"], resolve),
      },
    ]
  },
  // 记账模块
  {
    path: '/money',
    component: (resolve) => require(["@/views/money"], resolve),
    children: [
      {
        path: '',
        component: (resolve) => require(["@/views/money/money"], resolve),
      },
      {
        path: 'labels',
        component: (resolve) => require(["@/views/money/Labels"], resolve),
      },
      {
        path: 'details',
        component: (resolve) => require(["@/views/money/Details"], resolve),
      }
    ]
  },
  // 404页面
  {
    path: '*',
    component: (resolve) => require(["@/views/system/NotFound"], resolve),
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if(Route_White_List.indexOf(to.path) >= 0) {
    next();
  } else {
    if(localStorage.getItem('user')) {
      next()
    } else {
      next('/login')
    }
  }
})

router.afterEach((to, from, next) => {

});

export default router
